//
//  BGFindPwdViewController.m
//  bigMall
//
//  Created by ios on 16/7/20.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGFindPwdViewController.h"
#import "BGDateResult.h"
//#import "UIImageView+WebCache.h"
#import "BGBasicUrl.h"
#import "BGHttpTool.h"
#import "AFNetworking.h"
@interface BGFindPwdViewController () {
    
    UITextField *phoneTextFiled;//手机号
    UITextField *imageTextFiled;//图文
    UITextField *messageFiled;//短信
    UITextField *pwdTextFiled;//密码
    UITextField *querenpwdTextFiled;//确认密码
    UIButton *messageBtn;//获取验证码按钮
    UIImageView *imageView;//图文的变化
    NSTimer *_timer;
}

@end

@implementation BGFindPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"找回密码";
    
    [self createUI];
    
}

- (void)createUI {
    
    for (int i = 1; i <= 6; i++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(21, (i - 1) * 64 + 64, self.view.bounds.size.width - 42, 1)];
        
        if (i == 1) {
            
            view.hidden = YES;
            
        }
        
        view.backgroundColor = [UIColor lightGrayColor];
        
        view.alpha = 0.3;
        
        [self.view addSubview:view];
        
    }
    
    //phone
    UILabel *phoneLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64 - 64 + 64, BGWidth / 4, 64 / 3)];
    
    phoneLab.text = @"手机号:";
    phoneLab.font = [UIFont systemFontOfSize:15];
    phoneLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:phoneLab];
    
    phoneTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(phoneLab.right, phoneLab.y, 140, phoneLab.height)];
    
    phoneTextFiled.clearButtonMode = UITextFieldViewModeAlways;
    phoneTextFiled.placeholder = @"请输入手机号";
    
    [self.view addSubview:phoneTextFiled];
    
    //iamge
    UILabel *iamgeLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 256 / 3 + 65 - 64 + 64, BGWidth / 4, 64 / 3)];
    
    iamgeLab.text = @"图文验证码:";
    iamgeLab.font = [UIFont systemFontOfSize:15];
    iamgeLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:iamgeLab];
    
   imageTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(iamgeLab.right, iamgeLab.y, BGWidth / 2.5, iamgeLab.height)];
    
    imageTextFiled.placeholder = @"请输验证码";

    [self.view addSubview:imageTextFiled];
    
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageTextFiled.right, iamgeLab.y - 15, BGWidth - 21 - imageTextFiled.right, iamgeLab.height + 30)];
    
    imageView.contentMode = UIViewContentModeScaleToFill;
    
    imageView.userInteractionEnabled = YES;
    
    [self.view addSubview:imageView];
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    
    [tap addTarget:self action:@selector(imageClick)];
    
    [self imageClick];
    
    [imageView addGestureRecognizer:tap];
    
    //message
    UILabel *messageLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 * 7 / 3 + 66 - 64 + 64, BGWidth / 4, 64 / 3)];
    
    messageLab.text = @"短信验证码:";
    messageLab.font = [UIFont systemFontOfSize:15];
    messageLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:messageLab];
    
    messageFiled = [[UITextField alloc] initWithFrame:CGRectMake(messageLab.right, messageLab.y, BGWidth / 2.5, messageLab.height)];
    
    messageFiled.placeholder = @"请输入验证码";
    
//    messageFiled.secureTextEntry = YES;
    
    [self.view addSubview:messageFiled];
    
    messageBtn = [[UIButton alloc] initWithFrame:CGRectMake(messageFiled.right, messageLab.y - 15, BGWidth - 21 - messageFiled.right, messageLab.height + 30)];
    
    [messageBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    
    messageBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    
    messageBtn.tintColor = [UIColor whiteColor];
    
    messageBtn.backgroundColor = [UIColor redColor];
    
    messageBtn.clipsToBounds = YES;
    
    messageBtn.layer.cornerRadius = 10;
       
    messageBtn.selected = NO;
    
    [messageBtn addTarget:self action:@selector(clickMessage) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:messageBtn];
    
    //pwd
    UILabel *pwdLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 10 * 64 / 3 + 65 - 64 + 64, BGWidth / 4, 64 / 3)];
    
    pwdLab.text = @"密码:";
    pwdLab.font = [UIFont systemFontOfSize:15];
    pwdLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:pwdLab];
    
    pwdTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(pwdLab.right, pwdLab.y, 140, pwdLab.height)];

    pwdTextFiled.placeholder = @"请输入密码";
    
    [self.view addSubview:pwdTextFiled];

    //确认的pwd
    UILabel *querenpwdLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 13 * 64 / 3 + 66 - 64 + 64, BGWidth / 4, 64 / 3)];
    
    querenpwdLab.text = @"确认密码:";
    querenpwdLab.font = [UIFont systemFontOfSize:15];
    querenpwdLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:querenpwdLab];
    
    querenpwdTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(querenpwdLab.right, querenpwdLab.y, 160, querenpwdLab.height)];
    
    querenpwdTextFiled.placeholder = @"请确认密码";
    
    [self.view addSubview:querenpwdTextFiled];
    
    //规定密码的范围
    UITextField *provideFiled = [[UITextField alloc] initWithFrame:CGRectMake(21, 15 * 64 / 3 + 77 - 64 + 64, self.view.bounds.size.width - 42, 12)];
    
    provideFiled.placeholder = @"(密码长度6-16位,数字、字母、字符至少包含两种)";
    
    provideFiled.textAlignment = NSTextAlignmentCenter;
    
    provideFiled.userInteractionEnabled = NO;
    
    provideFiled.font = [UIFont systemFontOfSize:12];
    
    [self.view addSubview:provideFiled];
    
    //修改的Btn
    UIButton *xiugaiBtn = [[UIButton alloc] initWithFrame:CGRectMake(21, provideFiled.bottom + 37, self.view.bounds.size.width - 42, 50)];
    
    xiugaiBtn.clipsToBounds = YES;
    
    xiugaiBtn.layer.cornerRadius = 10;
    
    [xiugaiBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    
    [xiugaiBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    xiugaiBtn.backgroundColor = [UIColor redColor];
    
    [xiugaiBtn addTarget:self action:@selector(xiugai) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:xiugaiBtn];
    
}
- (void)startTimer {
    
    //timer
    _timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(restoreBtn) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
}
- (void)imageClick {
    
    NSString *url = [BGBasicUrl getBasicUrl];
    
    [BGHttpTool POSTImage:[NSString stringWithFormat:@"%@/index.php?s=/bigmall/tools/imgvc/type/forgetLoginPwd/id/1.html",url] paramenters:nil progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        
        imageView.image = [UIImage imageWithData:responseObject];
        
    } failure:^(NSError *error) {
        
    }];
    
    NSLog(@"%@",[NSString stringWithFormat:@"%@/bigmall/tools/imgvc/type/forgetLoginPwd",url]);

}
- (void)restoreBtn {
    
    messageBtn.enabled = YES;
    [messageBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    messageBtn.backgroundColor = [UIColor redColor];
    
    if (messageBtn.enabled == YES) {
        [_timer invalidate];
    }
    
}
- (void)clickMessage {
    NSLog(@"%@",imageTextFiled.text);
    
    [BGDateResult getPhoneCodeWithPhoneNum:phoneTextFiled.text andPicCode:imageTextFiled.text andType:@"forgetLoginPwd" success:^(NSDictionary *dic) {
                
                if ([dic[@"status"] isEqual:@"1"]) {
                    
                    [messageBtn  setTitle:@"已发送" forState:UIControlStateNormal];
                    
                    messageBtn.backgroundColor = [UIColor lightGrayColor];
                    
                    messageBtn.selected = YES;
                    
                    [self startTimer];
                    
                    [self showMessage:dic[@"msg"]];
                    
                } else {
                   
                    [self showMessage:dic[@"msg"]];
                    [self imageClick];
                }
                
            } failure:^(NSError *error) {
                
            }];
    
}

- (void)xiugai {
    
    [BGDateResult forgetLoginPwdWithPhone:phoneTextFiled.text andPwd:pwdTextFiled.text andAgapwd:querenpwdTextFiled.text andType:@"forgetLoginPwd" andPhoneCode:messageFiled.text success:^(NSString *status, NSString *msg, NSString *autoRes) {
        
        if (![status isEqual:@"1"]) {
            
            [self showMessage:msg];
            
        }else {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [phoneTextFiled resignFirstResponder];
    
    [imageTextFiled resignFirstResponder];
    
    [messageFiled resignFirstResponder];
    
    [pwdTextFiled resignFirstResponder];
    
    [querenpwdTextFiled resignFirstResponder];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
